package com.ruoyi.web.server;



import com.github.pagehelper.util.StringUtil;
import com.ruoyi.web.constant.Constants;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;


public class MessageBody {
    /**
     * 模板id
     */
    private String templateId;

    /**
     * 用户信息（与userType对应）
     */
    private String userInfo;
    /**
     * 1：账号 2： 手机号 3：身份证号
     */
    private Integer userType;
    /**
     * 2：个推（默认传2）
     */
    private int pushType;

    /**
     * 消息标题
     */
    private String title;

    /**
     * 消息跳转链接
     */
    private String url;

    /**
     * 模板变量
     */
    private Map<String, Object> templateWord;

    /**
     * 文件数量（没有不传）
     */
    private int fileNum;
    /**
     * 文件iid（没有不传，多个文件以‘,’分割，需要调用文件上传接口获取）
     */
    private String fileIids;

    /**
     * 扩展参数
     */
    private Map<String, Object> extendWord;

    /**
     * 批次号
     */
    private String batchNo;

    public MessageBody() {
    }

    public MessageBody(Builder builder) {
        this.templateId = builder.templateId;
        this.userInfo = builder.userInfo;
        this.userType = builder.userType;
        this.pushType = builder.pushType;
        this.title = builder.title;
        this.url = builder.url;
        this.templateWord = builder.templateWord;
        this.fileNum = builder.fileNum;
        this.fileIids = builder.fileIids;
        this.extendWord = builder.extendWord;
        this.batchNo = builder.batchNo;
        this.check();
    }

    /**
     * 校验
     */
    public void check() {
        if (StringUtil.isNotEmpty(this.batchNo)) {
            Matcher matcher = Constants.PATTERN.matcher(this.batchNo);
            if (!matcher.matches()) {
                throw new IllegalArgumentException("批次号格式不正确");
            }
        }
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(String userInfo) {
        this.userInfo = userInfo;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public int getPushType() {
        return pushType;
    }

    public void setPushType(int pushType) {
        this.pushType = pushType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, Object> getTemplateWord() {
        return templateWord;
    }

    public void setTemplateWord(Map<String, Object> templateWord) {
        this.templateWord = templateWord;
    }

    public int getFileNum() {
        return fileNum;
    }

    public void setFileNum(int fileNum) {
        this.fileNum = fileNum;
    }

    public String getFileIids() {
        return fileIids;
    }

    public void setFileIids(String fileIids) {
        this.fileIids = fileIids;
    }

    public Map<String, Object> getExtendWord() {
        return extendWord;
    }

    public void setExtendWord(Map<String, Object> extendWord) {
        this.extendWord = extendWord;
    }

    public String getBatchNo() {
        return batchNo;
    }

    public void setBatchNo(String batchNo) {
        this.batchNo = batchNo;
    }

    public static final class Builder {
        private String templateId;
        private String userInfo;
        private Integer userType;
        private int pushType;
        private String title;
        private String url;
        private Map<String, Object> templateWord = new HashMap<>(16);
        private int fileNum;
        private String fileIids;

        private Map<String, Object> extendWord = new HashMap<>(16);
        private String batchNo;

        public Builder() {
        }

        public Builder templateId(String templateId) {
            this.templateId = templateId;
            return this;
        }

        public Builder userInfo(String userInfo) {
            this.userInfo = userInfo;
            return this;
        }

        public Builder userType(Integer userType) {
            this.userType = userType;
            return this;
        }

        public Builder pushType(int pushType) {
            this.pushType = pushType;
            return this;
        }

        public Builder title(String title) {
            this.title = title;
            return this;
        }

        public Builder url(String url) {
            this.url = url;
            return this;
        }

        public Builder extendWord(String key, Object value) {
            this.extendWord.put(key, value);
            return this;
        }

        public Builder extendWord(Map<String, Object> extendWord) {
            this.extendWord = extendWord;
            return this;
        }

        public Builder fileNum(int fileNum) {
            this.fileNum = fileNum;
            return this;
        }

        public Builder fileIids(String fileIids) {
            this.fileIids = fileIids;
            return this;
        }

        public Builder templateWord(String key, Object value) {
            this.templateWord.put(key, value);
            return this;
        }

        public Builder templateWord(Map<String, Object> templateWord) {
            this.templateWord = templateWord;
            return this;
        }

        public Builder batchNo(String batchNo) {
            this.batchNo = batchNo;
            return this;
        }

        public MessageBody build() {
            MessageBody messageBody = new MessageBody(this);
            return messageBody;
        }
    }
}
